package com.tm.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.tm.CoreConstants;
import com.tm.ResultEntity;

/**
 * 上传工具类
 * @author GXL
 *
 */
public class UploadUtilsEx {
	

	/**
	 * 文件上传
	 * @param file
	 * @param folder
	 * @return
	 */
	public static ResultEntity savefile(MultipartFile file, String folder, boolean isAttach,
			HttpServletRequest request) {
		ResultEntity re = null;
		try {
			if (file.isEmpty()) {
		    	re = new ResultEntity("-1", "文件为空");
		    }
			
			String v_folder = folder == null || !StringUtils.hasText(folder) ? ""
					: ("/" + folder + "/");
			
			if(file.getSize() < 1048576){
				// 获取文件名
			    String fileName = file.getOriginalFilename();
			    long size = file.getSize();
			    System.out.println("上传的文件名为：" + fileName);
			    // 获取文件的后缀名
			    String suffix = fileName.substring(fileName.lastIndexOf("."));
			    System.out.println("上传的后缀名为：" + suffix);
			    System.out.println("上传文件的大小：" + file.getSize());
				
				// 文件夹
				String filepath = "";
				Date dt = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
				filepath = sdf.format(dt);

				String path = CoreConstants.FTP_RES_IMG_UPLOADPATH + v_folder + filepath + "/";//上传至目标服务器
//				String path = request.getSession().getServletContext().getRealPath("/") + CoreConstants.FTP_RES_IMG_UPLOADPATH + v_folder + filepath + "/";//上传至项目服务器

				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
				String filename = sdf1.format(dt);

				filename += suffix;
				File dest = new File(path + filename);
				// 检测是否存在目录
				if (!dest.getParentFile().exists()) {
					dest.getParentFile().mkdirs();
				}

				file.transferTo(dest);
				
				if(isAttach){
					//返回附件信息
					Map<String, String> res = new HashMap<String, String>();
//					res.put("path", path + filename);
//					res.put("path", "/" + CoreConstants.FTP_RES_IMG_UPLOADPATH + v_folder + filepath + "/" + filename);
					res.put("path", "/" + CoreConstants.FTP_SQL_IMG_UPLOADPATH + v_folder + filepath + "/" + filename);
					res.put("filetype", suffix);
					res.put("filename", filename);
					res.put("filesize", String.valueOf(size));
					
					re = new ResultEntity(res);
				}else
					re = new ResultEntity("0", "", "/" + CoreConstants.FTP_SQL_IMG_UPLOADPATH + v_folder + filepath + "/" + filename);
//					re = new ResultEntity("0", "", path + filename);
			}else{
				re = new ResultEntity("-1", "文件太大");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常");
		}
		
		return re;
	}
	
	
	/**
	 * Base64转换成文件并上传至服务器
	 * @param file
	 * @param folder
	 * @return
	 */
	public static ResultEntity saveBase64File(String imageStr, String folder, boolean isAttach,
			HttpServletRequest request) {
		ResultEntity re = null;
		try {
			if (imageStr.isEmpty()) {
		    	re = new ResultEntity("-1", "文件为空");
		    }
			
			String v_folder = folder == null || !StringUtils.hasText(folder) ? ""
					: ("/" + folder + "/");
			
			// 文件夹
			String filepath = "";
			Date dt = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
			filepath = sdf.format(dt);

			String path = CoreConstants.FTP_RES_IMG_UPLOADPATH + v_folder + filepath + "/";//上传至目标服务器
//			String path = request.getSession().getServletContext().getRealPath("/") + CoreConstants.FTP_RES_IMG_UPLOADPATH + v_folder + filepath + "/";//上传至项目服务器

			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			String filename = sdf1.format(dt);

			filename += ".jpg";
			
			File dest = new File(path + filename);
			// 检测是否存在目录
			if (!dest.getParentFile().exists()) {
				dest.getParentFile().mkdirs();
			}
			
			boolean flag = Base64UtilEx.GenerateImage(imageStr, path + filename);
			
			if(flag){
				if(isAttach){
					//返回附件信息
					Map<String, String> res = new HashMap<String, String>();
//						res.put("path", path + filename);
//					res.put("path", "/" + CoreConstants.FTP_RES_IMG_UPLOADPATH + v_folder + filepath + "/" + filename);
					res.put("path", "/" + CoreConstants.FTP_SQL_IMG_UPLOADPATH + v_folder + filepath + "/" + filename);
					res.put("filetype", ".jpg");
					res.put("filename", filename);
					
					re = new ResultEntity(res);
				}else
					re = new ResultEntity("0", "", "/" + CoreConstants.FTP_SQL_IMG_UPLOADPATH + v_folder + filepath + "/" + filename);
//					re = new ResultEntity("0", "", path + filename);
			}else{
				re = new ResultEntity("-1", "图片转换失败");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常");
		}
		
		return re;
	}
	
}
